package com.reservation.taxi.taxi.utils;


import cn.hutool.core.codec.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;

public class FileUtils {

    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\lianke-01\\Desktop\\非实时性报错文件\\非实时性报错文件\\驾驶员合同.pdf");
        String s = Base64.encode(file);
        System.err.println(s);
    }

    /**
     * 给文件base64位加密
     *
     * @param filePath 文件路径
     * @return
     */
    public static String GetFileStr(String filePath) {
        InputStream in = null;
        byte[] data = null;

        try {
            in = new FileInputStream(filePath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (Exception e) {

        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

    /**
     * 解密base64位加密的文件
     *
     * @param encode   加密字符串
     * @param savePath 保存地址
     * @param fileName 保存的文件名称（xx.xx）
     * @return
     */
    public static void SaveFile(String encode, String savePath, String fileName) throws IOException {
        BASE64Decoder encoder = new BASE64Decoder();
        byte[] bytes = encoder.decodeBuffer(encode);

        String filePath = savePath + "\\" + fileName;
        OutputStream out = new FileOutputStream(filePath);
        out.write(bytes);
        out.flush();
        out.close();
    }


}
